package com.fs.activiti5.handler;

import java.util.Arrays;

import org.activiti.engine.RuntimeService;
import org.activiti.engine.delegate.DelegateTask;
import org.activiti.engine.delegate.TaskListener;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import com.fs.activiti5.service.ActivitiService;
import com.fs.base.param.SysConstant;
import com.fs.base.pojo.Org;
import com.fs.base.pojo.User;
import com.fs.base.repository.UserRepository;
import com.fs.base.utils.ApplicationUtils;
import com.fs.jzzn.pojo.Expense;
import com.fs.jzzn.repository.ExpenseRepository;

/**
 * @author TiddlerCJ
 *
 */
@Component
public class ExpenseHandler implements TaskListener {
	private static final long serialVersionUID = 1L;
	
	public void notify(DelegateTask delegateTask) {
		ActivitiService activitiService = (ActivitiService) ApplicationUtils.getBean("activitiService");
		UserRepository userRepository = (UserRepository) ApplicationUtils.getBean("userRepository");
		
		String taskName = delegateTask.getName();
		
		//更新工程还支出表单状态
		RuntimeService runtimeService = activitiService.getRuntimeService();
		Object expenseId = runtimeService.getVariable(delegateTask.getExecutionId(), "expenseId");
		if(expenseId!=null){
			ExpenseRepository repository = activitiService.getExpenseRepository();
			Expense expense = repository.findOne(expenseId.toString());
			expense.setStatus(taskName);
			expense.setProcessInstanceId(delegateTask.getProcessInstanceId());
			repository.save(expense);
			/*
			 * 通过当前提交记录人所属组织获取组织负责人
			 */
			User user = userRepository.findOne(expense.getCreaterId());
			String orgNo = user.getOrgNo();
			/*
			 * 采用组织机构的org编号为依据
			 */
			if(StringUtils.isNotBlank(expense.getOrgNo())){
				orgNo = expense.getOrgNo();
			}
			
			String inputUser = "系统管理员";
			switch (taskName) {
				case "填写表单":
					inputUser = expense.getCreaterName();
					break;
				case "合规性检查":
					//inputUser = "张卓,杨芳";
					inputUser = SysConstant.USER_CONFIG_MAP.get("hg");
					break;
				case "部门审批":
					Org org = activitiService.getOrgRepository().findByOrgNo(orgNo); 
					inputUser = org.getOrgLeaderName();
					break;
				case "大区审批":
					String projectOrgNo = expense.getOrgNo();
					if(StringUtils.isNotBlank(projectOrgNo)){//浙江
						 if(projectOrgNo.startsWith("000002")){
							 //inputUser = "游斌";
							 inputUser = SysConstant.USER_CONFIG_MAP.get("zj");
						 }else if(projectOrgNo.startsWith("000003")){
							 inputUser = SysConstant.USER_CONFIG_MAP.get("hb");
							 //inputUser = "余春华";
							 if(projectOrgNo.startsWith("00000309")){
								 inputUser = SysConstant.USER_CONFIG_MAP.get("hh");
								 //inputUser = "朱英俊";
							 }
						 }else{
							 //inputUser = "系统管理员";
							 inputUser = SysConstant.USER_CONFIG_MAP.get("hg") + ",未指定";
						 }
						 if(projectOrgNo.equals("0000030303")){//十堰项目部
							 inputUser = "张卓";
						 }
					}
					break;
				/*case "大区负责人2":
					if(StringUtils.isNotBlank(orgNo)){//浙江
						 if(orgNo.startsWith("000002")){
							 inputUser = "朱英俊,游斌";
						 }else if(orgNo.startsWith("000003")){
							 inputUser = "李念念";
						 }else if(orgNo.startsWith("00000309")){
							 inputUser = "朱英俊";
						 }
						 else{
							 inputUser = "田婵娟";
						 }
					}
					break;*/
				case "财务审批":
					 inputUser = "田婵娟";
					break;
				default: break;
			}
			if(StringUtils.isBlank(inputUser)){
				inputUser = "系统管理员";
			}
			
			String [] candidateUsers=  inputUser.split(",") ;  
			delegateTask.addCandidateUsers(Arrays.asList(candidateUsers));
		}
		/*
		 * 加快回收
		 */
		activitiService = null;
		userRepository = null;
	}
}
